package com.zy.mc_module.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zy.mc_module.base.App;
import com.zy.mc_module.common.Constant;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created on 18/10/23
 * SharedPreferences 存储工具类
 */
public class SPUtil {

    /**
     * 保存在手机里面的文件名
     */
    public static final String FILE_NAME = "ppyl_sp";
    private static SharedPreferences sp;
    private static final Gson gson = new Gson();

    public static void init(Context mContext) {
        sp = mContext.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
    }

    public static SharedPreferences getSp() {
        if (null == sp) {
            sp = App.getInstance().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        }
        return sp;
    }

    /**
     * 存储字符串
     */
    public static void putString(String key, String value) {
        getSp().edit().putString("String-" + key, value).apply();
    }

    /**
     * 取出字符串
     */
    public static String getString(String key, String defValue) {
        return getSp().getString("String-" + key, defValue);
    }

    /**
     * 存储整型
     */
    public static void putInt(String key, int value) {
        getSp().edit().putInt("Int-" + key, value).apply();
    }

    /**
     * 取出整型
     */
    public static int getInt(String key) {
        return getSp().getInt("Int-" + key, 0);
    }

    /**
     * 取出整型
     */
    public static int getInt(String key, int defValue) {
        return getSp().getInt("Int-" + key, defValue);
    }

    /**
     * 存储Boolean类型
     */
    public static void putBoolean(String key, boolean value) {
        getSp().edit().putBoolean("Boolean-" + key, value).apply();
    }

    /**
     * 取出Boolean类型 默认值为false
     */
    public static boolean getBoolean(String key) {
        return getSp().getBoolean("Boolean-" + key, false);
    }

    /**
     * 对象存储
     *
     * @param obj 对象
     */
    public static void putObject(String key, Object obj) {
        getSp().edit().putString("Object-" + key, gson.toJson(obj)).apply();
    }

    /**
     * 取出对象
     */
    public static <T> T getObject(String key, Class<T> clazz) {
        String objJson = getSp().getString("Object-" + key, null);
        return objJson != null ? gson.fromJson(objJson, clazz) : null;
    }

    /**
     * 存储集合List
     */
    public static <T> void putList(String key, List<T> lists) {
        if (null == lists || lists.size() <= 0)
            return;
        //转换成json数据，再保存
        String listJson = gson.toJson(lists);
        getSp().edit().putString("List-" + key, listJson).apply();
    }

    /**
     * 取出集合List
     */
    public static <T> List<T> getList(String key, Type type) {
        String listJson = getSp().getString("List-" + key, "");
        List<T> lists = gson.fromJson(listJson, type);
        if (null != lists) {
            return lists;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 移除某个key值已经对应的值
     */
    public static void removeObje(String key) {
        getSp().edit().putString("Object-" + key, "").apply();
    }

    /**
     * 移除某个key值已经对应的值
     */
    public static void removeList(String key) {
        getSp().edit().putString("List-" + key, "").apply();
    }

    /**
     * 清除所有数据
     */
    public static void clear() {
        getSp().edit().clear().commit();
    }


    //------------------------------

    /**
     * 储存搜索记录
     */
    public static void saveSearch(String text) {
        if (!TextUtils.isEmpty(text)) {
            List<String> searchList = getList(Constant.SEARCH_HISTORY, new TypeToken<List<String>>() {
            }.getType());
            if (searchList.contains(text)) {
                searchList.remove(text);
            }
            searchList.add(0, text);
            //最多存储20条数据
            for (int i = 0; i < searchList.size(); i++) {
                if (i > 19) {
                    searchList.remove(i);
                }
            }
            putList(Constant.SEARCH_HISTORY, searchList);
        }
    }

    /**
     * 获取搜索记录
     */
    public static List<String> getSearch() {
        return getList(Constant.SEARCH_HISTORY, new TypeToken<List<String>>() {
        }.getType());
    }

    /**
     * 清除搜索数据
     */
    public static void cleanSearch() {
        removeList(Constant.SEARCH_HISTORY);
    }

    /**
     * 储存搜索记录
     */
    public static void saveWLSearch(String text) {
        if (!TextUtils.isEmpty(text)) {
            List<String> searchList = getList("WL" + Constant.SEARCH_HISTORY, new TypeToken<List<String>>() {
            }.getType());
            if (searchList.contains(text)) {
                searchList.remove(text);
            }
            searchList.add(0, text);
            //最多存储20条数据
            for (int i = 0; i < searchList.size(); i++) {
                if (i > 19) {
                    searchList.remove(i);
                }
            }
            putList("WL" + Constant.SEARCH_HISTORY, searchList);
        }
    }

    /**
     * 获取搜索记录
     */
    public static List<String> getWLSearch() {
        return getList("WL" + Constant.SEARCH_HISTORY, new TypeToken<List<String>>() {
        }.getType());
    }

    /**
     * 清除搜索数据
     */
    public static void cleanWLSearch() {
        removeList("WL" + Constant.SEARCH_HISTORY);
    }
}
